<?php
/**
 * Description: 获取考试项目列表
 * User: devforma
 * Date: 15/3/31
 * Time: 20:59
 */

class GetExams implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;
        
        $sql = 'SELECT e.exam_name, ed.id, e.shortening, ed.is_official, ed.exam_day FROM `data_exams` AS e INNER JOIN `data_exam_detail` AS ed
        ON e.exam_id=ed.exam_id AND ed.type="笔试" AND UNIX_TIMESTAMP(ed.exam_day)>'.time().' ORDER BY ed.exam_day ASC';
        $exams = $cmsdb->getResults($sql);

        $examList = array();
        foreach ($exams as $exam) {
            if (!isset($examList[$exam->shortening])) { //只取最近的第一个考试
                $examList[$exam->shortening] = array(
                    'name' => $exam->exam_name,
                    'code' => $exam->shortening,
                    'exam_id' => intval($exam->id),
                    'is_official' => $exam->is_official ? true : false,
                    'is_interview' => false,
                    'date' => $exam->exam_day
                );
            }
        }

        $examList = array_values($examList);

        return genOutput(1, array(
            'exams' => $examList
        ));
    }
}